package de.eventdroid;

import java.util.ArrayList;

import de.eventdroid.helper_classes.GeoDataHandler;
import de.eventdroid.helper_classes.JSONHandler;
import de.eventdroid.helper_classes.RESTHandler;
import de.eventdroid.helper_classes.TextHandler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EventManageOverviewScreen extends Activity {
    
	/* Loading dialog */
	ProgressDialog dialog;
	
    /**
	 * Declaration of needed variables
	 */
	private String resultString = null;
    private Intent actualIntent;
	private ArrayList<String> aktFoundObjectsForListViewArray = new ArrayList<String>();
	private ArrayList<String> aktFoundObjectIDs = new ArrayList<String>();
	private ListAdapter listenAdapter = null;
	private ArrayAdapter<String> eventArrayAdapter = null;
	private ListView aktListView;
	private String user_id;
    
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdroid_user_manage_events_screen);
        
        /* Get the actual User ID */
    	user_id = getIntent().getExtras().getString("user_id");
        
        this.fetchLocationResultsForListView();
        
        aktListView = (ListView)findViewById(R.id.listview_event_result_list_view);
        actualIntent = new Intent(this, EventCreateEditScreen.class);
		
    	aktListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View aktView, int clickedPosition,
					long arg3) {
				aktListView.getItemAtPosition(clickedPosition);	
				actualIntent.putExtra("mode", "update");
				actualIntent.putExtra("user_id", user_id);
				actualIntent.putExtra("event_id", aktFoundObjectIDs.get(clickedPosition));
				startActivity(actualIntent);
				
			}
		});
        
    }
    
    /**
     * Worker Class for fetching the Location Results for ListView 
     */
	private void fetchLocationResultsForListView() {
				
		/* request the data */
		/* TODO fetch USER EVENTS ONLY */
		resultString = RESTHandler.getData("eventHandler");
    	
    	/* Parse the JSON String into  */
    	aktFoundObjectsForListViewArray = JSONHandler.parseJSONDataForListView(resultString, "event", true);
    	
    	/* Extract Location IDs from content */
    	aktFoundObjectIDs = TextHandler.extractIDFromResultArray(aktFoundObjectsForListViewArray);
    	
    	/* Cleanup List Location from IDs */
    	aktFoundObjectsForListViewArray = TextHandler.cleanUpArrayFromIds(aktFoundObjectsForListViewArray);
    	
	    /* Allocate ListAdapter for filling the ListView */
	    aktListView = (ListView) findViewById(R.id.listview_event_result_list_view);
	    eventArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, aktFoundObjectsForListViewArray);
	    eventArrayAdapter.notifyDataSetChanged();
	    listenAdapter = eventArrayAdapter;
	    aktListView.setAdapter(listenAdapter);
	    
	    /* Number of found Locations in Toast */
	    Toast.makeText(getBaseContext(), "Es wurden "+aktFoundObjectsForListViewArray.size()+" Veranstaltungen gefunden." ,Toast.LENGTH_LONG).show();	
    }
    
    /** 
     * The Click EventHandler Methods for buttons on the Profile Overview Screen
     */   
    public void onEventCreationClick(final View view) {
    	
    	/* show loading dialog */
    	dialog = ProgressDialog.show(this, "", "Bitte warten, Sie werden weitergeleitet....", true);
    	
    	actualIntent = new Intent(this, EventCreateEditScreen.class);
    	actualIntent.putExtra("user_id", user_id);
    	actualIntent.putExtra("mode", "create");
    	dialog.cancel();
		startActivity(actualIntent);
    }
    
    /**
     * Creation Class for the menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_user, menu);
    	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
    		case R.id.menu_startscreen:     
    			startActivity(new Intent(this, StartScreen.class));           	
    			break;
    		case R.id.menu_user_overview:     
    			actualIntent = new Intent(this, ProfileOverviewScreen.class);
    			actualIntent.putExtra("user_id", user_id);
    			startActivity(actualIntent);           	
    			break;
    	}
    	return true;
    }
    
}